<?php

declare(strict_types=1);
/*
 * This file is part of Leonsw.
 *
 * @link     https://leonsw.com
 * @document https://docs.leonsw.com
 * @contact  leonsw.com@gmail.com
 * @license  https://leonsw.com/LICENSE
 */

namespace App\Repositories\Lottery;


use App\Models\MainDB\Lottery\DrawRecord as Model;

use App\Traits\Singleton;

class DrawRecordRepository
{
    use Singleton;

    public function detail(int $drawId)
    {
        return $this->one(['draw_id' => $drawId]);
    }

    public function get(array $filters = [])
    {
        $query = Model::query();
        $query->where($filters);
        $query->with(['gift']);
        return $query->get();
    }

    public function all(array $filters = [], int $page = 1, int $perPage = 100)
    {
        $query = Model::query();
        $query->where($filters);
        $query->with(['gift']);
        return $query->paginate($perPage, ['*'], 'page', $page);
    }

    public function one(array $filters, bool $fail = false)
    {
        $query = Model::query();
        $query->where($filters);
        return $fail ? $query->firstOrFail() : $query->first();
    }

    public function find(int $id)
    {
        return $this->one(['id' => $id], true);
    }

    public function create(array $data)
    {
        $model = new Model();
        $model->fill($data);
        $model->save();
        $model->refresh();

        return $model;
    }
}
